package cn.itrip.biz.service;

import cn.itrip.beans.domain.ItripHotelOrder;
import cn.itrip.beans.domain.ItripHotelRoom;
import cn.itrip.beans.domain.ItripUser;
import cn.itrip.beans.domain.ItripUserLinkUser;
import cn.itrip.beans.vo.ItripPersonalOrderRoomVO;
import cn.itrip.beans.vo.StoreVO;
import cn.itrip.common.ConfigManager;
import cn.itrip.common.Constants;
import cn.itrip.common.DataUtils;
import cn.itrip.common.RandomUtils;
import com.itrip.dao.ItripHotelOrderMapper;
import com.itrip.dao.ItripHotelRoomMapper;
import com.itrip.dao.ItripHotelTempStoreMapper;
import com.itrip.dao.ItripUserLinkUserMapper;
import org.apache.tools.ant.util.DateUtils;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HotelOrderService {
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripUserLinkUserMapper itripUserLinkUserMapper;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    public List<StoreVO> getpreorderinfo( Map<String, Object> params) {
        String checkInDate = params.get("checkInDate").toString();
        String checkOutDate = params.get("checkOutDate").toString();
        checkInDate=checkInDate.replaceAll("Z"," UTC");
        checkOutDate=checkOutDate.replaceAll("Z"," UTC");
        params.put("startTime",DataUtils.parse(checkInDate,"yyyy-MM-dd'T'HH:mm:ss.SSS Z"));
        params.put("endTime", DataUtils.parse(checkOutDate,"yyyy-MM-dd'T'HH:mm:ss.SSS Z"));
        itripHotelTempStoreMapper.flushStore(params);
        return itripHotelTempStoreMapper.queryRoomStore(params);
    }

    public List<ItripUserLinkUser> queryuserlinkuser(Long linkid) {
        return  itripUserLinkUserMapper.selectBylinkUserId(linkid);
    }
    public void addhotelorder(ItripHotelOrder order)throws Exception{
        int days = Days.daysBetween(LocalDate.fromDateFields(order.getCheckInDate()),
                LocalDate.fromDateFields(order.getCheckOutDate())).getDays();
        if(days < 1){
            throw  new Exception("订房天数不能小于1："+days);
        }
        order.setBookingDays(days);
        //设置订单编号
        String hostId = ConfigManager.getInstance().getValue("hostId");
        String orderNo = hostId + DataUtils.formate(new Date())+ RandomUtils.random(6);
        order.setOrderNo(orderNo);
        //设置订单状态
        order.setOrderStatus(Constants.ORDER_STATUS_WAIT_PAY);
        //计算支付金额
         Integer count= order.getCount();
        ItripHotelRoom hotelRoom = itripHotelRoomMapper.selectByPrimaryKey(order.getRoomId());
        BigDecimal amount = hotelRoom.getRoomPrice().multiply(new BigDecimal(days*count));
        order.setPayAmount(amount);
        if(amount.compareTo(new BigDecimal(0)) != 1){
            throw  new Exception("订单支付金额异常:"+amount);
        }
        itripHotelOrderMapper.insert(order);
    }

    public ItripPersonalOrderRoomVO getItripHotelOrderRoomInfoById(Long orderId) {
        return itripHotelOrderMapper.getItripHotelOrderRoomInfoById(orderId);
    }
}
